package com.zzyl.nursing.service.impl;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author zyl
 * @date 2025-02-21
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1.显示护理计划基本信息
        NursingPlan nursingPlan = getById(id);

        // 2.查询关联的所有护理项目
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId, id);
        List<NursingProjectPlan> list = nursingProjectPlanService.list(wrapper);
        List<NursingProjectPlanVo> projectPlanVos = BeanUtil.copyToList(list, NursingProjectPlanVo.class);
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        vo.setProjectPlans(projectPlanVos);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlandto 护理计划dto
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlandto) {
        // 1.保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlandto, nursingPlan);
        save(nursingPlan);

        // 2.保存护理计划项目关联信息
        List<NursingProjectPlan> nursingProjectPlans = nursingPlandto.getProjectPlans();
        nursingProjectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
        // return nursingPlanMapper.insertNursingPlan(nursingPlan);
        // return save(nursingPlandto) ? 1 : 0 ;
        nursingProjectPlanService.saveBatch(nursingProjectPlans);
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto, nursingPlan);
        updateById(nursingPlan);
        if (nursingPlanDto.getProjectPlans() != null && !nursingPlanDto.getProjectPlans().isEmpty()) {
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
            projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            // 如果 ids 为 null 或空数组，直接返回 0
            return 0;
        };
        // 删除关联的护理项目计划
        for (Long id : ids) {
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        }
        // 删除护理计划
        return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        // return nursingPlanMapper.deleteNursingPlanById(id);
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanVo> selectAll() {
        List<NursingPlan> list = this.list();
        return BeanUtil.copyToList(list,NursingPlanVo.class);
    }
}
